// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   LicenseManager.java

package com.jivesoftware.forum;

import com.jivesoftware.util.StringUtils;
import java.io.*;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.Date;

// Referenced classes of package com.jivesoftware.forum:
//            LicenseException, License, JiveGlobals

public final class LicenseManager
{

    private static License license = null;
    private static boolean error = false;
    private static String errorMsg;

    private LicenseManager()
    {
    }

    public static void checkAuthorized(String s, String s1)
        throws LicenseException
    {
        loadLicense();
        if(!error)
        {
            int i = getProductScore(license.getProduct());
            int j = getProductScore(s);
            if(i < j)
                throw new LicenseException("You are not licensed to use this product.");
            if(license.getVersion().compareTo(s1) < 0)
                throw new LicenseException("Your are not licensed to use this version of the software.");
            else
                return;
        } else
        {
            throw new LicenseException(errorMsg);
        }
    }

    public static long getLicenseID()
    {
        return license.getLicenseID();
    }

    public static String getProduct()
    {
        return license.getProduct();
    }

    public static String getVersion()
    {
        return license.getVersion();
    }

    public static License.LicenseType getLicenseType()
    {
        return license.getLicenseType();
    }

    public static String getName()
    {
        return license.getName();
    }

    public static String getCompany()
    {
        return license.getCompany();
    }

    public static int getNumCopies()
    {
        return license.getNumCopies();
    }

    public static Date getExpiresDate()
    {
        if(license.getExpiresDate() == null)
            return null;
        else
            return new Date(license.getExpiresDate().getTime());
    }

    static boolean validate(License license1)
        throws Exception
    {
        String s = "308201b73082012c06072a8648ce3804013082011f02818100fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c70215009760508f15230bccb292b982a2eb840bf0581cf502818100f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a038184000281800af088055b9304337d285bd946e095465b6d16472e8fc4c29ab735d46fed6140236a3227a2afce0ec54d0002cb95a4988a3e545b1c58c030515c24ecc3de6763fca3f09e05ca568a594d370b879a338043ee3c5a6fe26e95c2b749ce4a8150cd61dd0459e6d5d0862a1ca857c8efc55c73ef3e883ca89eb8358b7147d06d854a";
        byte abyte0[] = StringUtils.decodeHex(s);
        X509EncodedKeySpec x509encodedkeyspec = new X509EncodedKeySpec(abyte0);
        KeyFactory keyfactory = KeyFactory.getInstance("DSA");
        java.security.PublicKey publickey = keyfactory.generatePublic(x509encodedkeyspec);
        Signature signature = Signature.getInstance("DSA");
        signature.initVerify(publickey);
        signature.update(license1.getFingerprint());
        return signature.verify(StringUtils.decodeHex(license1.getSignature()));
    }

    private static synchronized void loadLicense()
    {
        if(license != null)
            return;
        File file = new File(JiveGlobals.getJiveHome(), "jive.license");
        if(!file.exists())
        {
            error = true;
            errorMsg = "The license file could not be found.";
            return;
        }
        byte abyte0[] = new byte[0];
        try
        {
            BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
            StringBuffer stringbuffer = new StringBuffer(100);
            String s;
            while((s = bufferedreader.readLine()) != null) 
                stringbuffer.append(s);
            bufferedreader.close();
            String s1 = StringUtils.decodeBase64(stringbuffer.toString());
            license = License.fromXML(s1);
            if(license.getExpiresDate() != null)
            {
                long l = System.currentTimeMillis();
                if(license.getExpiresDate().getTime() < l)
                {
                    error = true;
                    errorMsg = "Your license has expired.";
                    return;
                }
            }
            if(!validate(license))
            {
                error = true;
                errorMsg = "Your license file does not appear to be valid.";
                return;
            }
        }
        catch(Exception exception)
        {
            error = true;
            errorMsg = "Error reading license file.";
            return;
        }
        error = false;
    }

    private static int getProductScore(String s)
    {
        if("Jive Forums Basic".equals(s))
            return 1;
        if("Jive Forums Professional".equals(s))
            return 2;
        return !"Jive Forums Enterprise".equals(s) ? 0 : 3;
    }

}
